package com.gking.processHarvest.service.Example;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.CustomException;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.entity.Example;
import com.gking.processHarvest.entity.Notes;
import com.gking.processHarvest.mapper.ExampleMapper;
import com.gking.processHarvest.service.Notes.NotesService;
import com.gking.processHarvest.utils.CheckStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class ExampleServiceImpl extends ServiceImpl<ExampleMapper, Example> implements ExampleService {


    @Autowired
    private NotesService notesService;

    /**
     * @param projectId   项目id
     * @param harvestId   收获id
     * @param showContent 展现内容
     * @param editContent 编辑内容
     * @return s
     */
    @Override
    public Res<String> create(String projectId, String harvestId, String showContent, String editContent) {
        if (projectId == null || harvestId == null) return Res.error("请将参数补充完整！");

        Example example = new Example();
        example.setProjectId(projectId);
        example.setHarvestId(harvestId);
        example.setContent(showContent);
        example.setEditContent(editContent);
        this.save(example);

        return Res.success("创建成功！");
    }


    /**
     * 更新示例
     *
     * @param exampleId   示例id
     * @param showContent 展现内容
     * @param editContent 编辑内容
     * @return s
     */
    @Override
    public Res<String> update(String exampleId, String showContent, String editContent) {
        if (exampleId == null || showContent == null || editContent == null) return Res.error("请将参数补充完整！");

        //是否存在且未删除
        CheckStatus<Example> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Example> queryWrapper = checkStatus.checkIsDelete(
                exampleId, Example::getId,
                Example::getMasterId, Example::getIsDelete);
        if (this.getOne(queryWrapper) == null) return Res.error("修改示例失败！");

        //是，则更新
        LambdaUpdateWrapper<Example> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Example::getId, exampleId);
        wrapper.eq(Example::getMasterId, BaseContext.getCurrentId());
        wrapper.set(Example::getContent, showContent);
        wrapper.set(Example::getEditContent, editContent);
        this.update(wrapper);

        return Res.success("更新示例成功！！");
    }


    /**
     * 删除示例
     *
     * @param exampleId 示例id
     * @return s
     */
    @Override
    @Transactional
    public Res<String> delete(String exampleId) {
        if (exampleId == null || exampleId.equals("")) return Res.error("请将id补充完整！");

        //是否存在且未删除
        CheckStatus<Example> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Example> queryWrapper = checkStatus.checkIsDelete(
                exampleId, Example::getId,
                Example::getMasterId, Example::getIsDelete);
        if (this.getOne(queryWrapper) == null) return Res.error("删除示例失败！");

        //是，则删除
        LambdaUpdateWrapper<Example> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Example::getId, exampleId);
        updateWrapper.eq(Example::getMasterId, BaseContext.getCurrentId());
        updateWrapper.set(Example::getIsDelete, 1);
        updateWrapper.set(Example::getDeleteTime, LocalDateTime.now());
        this.update(updateWrapper);

        try {
            //删除示例下的所有笔记
            CheckStatus<Notes> checkNotes = new CheckStatus<>();
            LambdaQueryWrapper<Notes> notesQueryWrapper = checkNotes.checkIsDelete(
                    exampleId, Notes::getExampleId,
                    Notes::getMasterId, Notes::getIsDelete);
            notesService.list(notesQueryWrapper).forEach(item -> notesService.delete(item.getId()));

        } catch (Exception e) {
            throw new CustomException("删除示例成功，删除项目下的子模块时出现未知错误！");
        }

        return Res.success("删除成功！");
    }


    /**
     * 获取示例的笔记列表
     *
     * @param exampleId 示例id
     * @return s
     */
    @Override
    public Res<List<Notes>> getNoteList(String exampleId) {

        //判断示例是否存在
        CheckStatus<Example> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Example> queryWrapper = checkStatus.checkIsDelete(
                exampleId, Example::getId,
                Example::getMasterId, Example::getIsDelete);
        if (this.getOne(queryWrapper) == null) return Res.error("此示例不存在，无法获取笔记列表！");

        //获取笔记列表
        CheckStatus<Notes> checkStatus2 = new CheckStatus<>();
        LambdaQueryWrapper<Notes> wrapper = checkStatus2.checkIsDelete(
                exampleId, Notes::getExampleId,
                Notes::getMasterId, Notes::getIsDelete);
        List<Notes> list = notesService.list(wrapper);

        return Res.success("获取笔记列表成功！", list);
    }


    /**
     * 获取示例详情
     *
     * @param exampleId 示例id
     * @return s
     */
    @Override
    public Res<Example> getExampleDetail(String exampleId) {
        CheckStatus<Example> checkStatus = new CheckStatus<>();
        LambdaQueryWrapper<Example> queryWrapper = checkStatus.checkIsDelete(
                exampleId, Example::getId,
                Example::getMasterId, Example::getIsDelete);

        Example one = this.getOne(queryWrapper);
        if (one == null) return Res.error("示例不存在");

        return Res.success("获取示例详情成功！", one);
    }

}
